package com.sch.www.codeGenerator.base;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface BaseMapper<Record extends BaseEntity, RecordWithBLOBs extends Record, Example extends BaseExample> {

    int countByExample(Example example);

    int deleteByExample(Example example);

    int deleteByPrimaryKey(String id);

    int insert(RecordWithBLOBs record);

    int insertSelective(RecordWithBLOBs record);

    List<Record> selectByExample(Example example);

    RecordWithBLOBs selectByPrimaryKey(String id);

    int updateByExampleSelective(@Param("record") RecordWithBLOBs record, @Param("example") Example example);

    int updateByExample(@Param("record") Record record, @Param("example") Example example);

    int updateByPrimaryKeySelective(RecordWithBLOBs record);

    int updateByPrimaryKey(Record record);

    default List<RecordWithBLOBs> selectByExampleWithBLOBs(Example example) {
        return null;
    }

    default int updateByExampleWithBLOBs(@Param("record") RecordWithBLOBs record, @Param("example") Example example) {
        return 0;
    }

    default int updateByPrimaryKeyWithBLOBs(RecordWithBLOBs record) {
        return 0;
    }


    default Record selectOneByExample(Example example) {
        List<Record> list = selectByExample(example);
        int size = list.size();
        if (size == 0) {
            return null;
        } else if (size == 1) {
            return list.get(0);
        } else {
            throw new RuntimeException("selectOneByExample从数据库中查询出多条数据。");
        }
    }

    default RecordWithBLOBs selectOneByExampleWithBLOBs(Example example) {
        List<RecordWithBLOBs> list = selectByExampleWithBLOBs(example);
        int size = list.size();
        if (size == 0) {
            return null;
        } else if (size == 1) {
            return list.get(0);
        } else {
            throw new RuntimeException("selectOneByExampleWithBLOBs从数据库中查询出多条数据。");
        }
    }

    default CustomPage<Record> selectByExampleOffsetPage(Example example, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Record> recordList = selectByExample(example);
        return new CustomPage<>((Page<Record>) recordList);
    }

    default CustomPage<Record> selectByExampleStartPage(Example example, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Record> recordList = selectByExample(example);
        return new CustomPage<>((Page<Record>) recordList);
    }

    default CustomPage<RecordWithBLOBs> selectByExampleWithBLOBsOffsetPage(Example example, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<RecordWithBLOBs> recordList = selectByExampleWithBLOBs(example);
        return new CustomPage<>((Page<RecordWithBLOBs>) recordList);
    }

    default CustomPage<RecordWithBLOBs> selectByExampleWithBLOBsStartPage(Example example, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<RecordWithBLOBs> recordList = selectByExampleWithBLOBs(example);
        return new CustomPage<>((Page<RecordWithBLOBs>) recordList);
    }


}
